﻿using Dycox.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dycox.Windows.Forms
{
    public partial class InputBox : Form
    {
        public InputBox()
        {
            InitializeComponent();
        }

        public string Help
        {
            get { return lbHelp.Text; }
            set { lbHelp.Text = value; }
        }

        public string Value
        {
            get
            {
                string value = textBox1.Text;

                if ((_options & InputBoxOptions.TrimWhiteSpaces) == InputBoxOptions.TrimWhiteSpaces)
                    value = value.Trim();

                return value;
            }
            set { textBox1.Text = value; }
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageSet set = new MessageSet();

            ValidateInput(Value, set);

            if (!set.Success)
            {
                MessageBoxIcon icon = MessageBoxIcon.Warning;

                if (set.Level == MessageKind.Error) icon = MessageBoxIcon.Error;

                MessageBox.Show(this, set.ToString(), null, MessageBoxButtons.OK, icon);
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void ValidateInput(string value, MessageSet msgSet)
        {
            if ((_options & InputBoxOptions.AllowsEmpty) == InputBoxOptions.Default && value.Length == 0)
                msgSet.Add("输入的内容不能为空", MessageKind.Warning);

        }

        public static string Show(IWin32Window parent, string help)
        {
            return Show(parent, help, InputBoxOptions.Default);
        }

        public static string Show(IWin32Window parent, string help, InputBoxOptions options)
        {
            return Show(parent, help, null, options);
        }

        public static string Show(IWin32Window parent, string help, string value , InputBoxOptions options) 
        {
            InputBox box = new InputBox();

            if (help != null)
                box.Help = help;

            if (value != null)
                box.Value = value;

            box.Options = options;

            if (box.ShowDialog(parent) == DialogResult.OK)
                return box.Value;
            else
                return null;
        }

        private InputBoxOptions _options;

        public InputBoxOptions Options
        {
            get { return _options; }
            set
            {
                if (_options != value)
                {
                    InputBoxOptions ch = _options ^ value;
                    _options = value;
                    ApplyOptionChages(ch);
                }
            }
        }

        private void ApplyOptionChages(InputBoxOptions changedOptions)
        {
            if ((changedOptions & InputBoxOptions.Multiline) == InputBoxOptions.Multiline)
                UpdateMultiline();
        }

        private const int MULTILINE_HEIGHT = 70;

        private void UpdateMultiline()
        {
            bool ml = ((_options & InputBoxOptions.Multiline) == InputBoxOptions.Multiline);

            if (ml != textBox1.Multiline)
            {
                if (ml)
                {
                    int h = textBox1.Height;
                    textBox1.Multiline = true;
                    textBox1.ScrollBars = ScrollBars.Both;
                    textBox1.Height = MULTILINE_HEIGHT;
                    textBox1.AcceptsReturn = true;

                    var size = this.ClientSize;
                    this.ClientSize = new Size(size.Width, size.Height + MULTILINE_HEIGHT - h);
                }
                else
                {
                    int h = textBox1.Height;

                    textBox1.Multiline = false;
                    textBox1.AcceptsReturn = false;
                    textBox1.ScrollBars = ScrollBars.None;

                    var size = this.ClientSize;
                    this.ClientSize = new Size(size.Width, size.Height - (h - textBox1.Height));
                }
            }
        }
    }

    [Flags]
    public enum InputBoxOptions
    {
        Default = 0,
        AllowsEmpty = 1,
        TrimWhiteSpaces = 2,
        Multiline = 4
    }
}
